import {Injectable} from '@angular/core';
import {RequestUtil} from "../../../util/request-util";
import {Observable} from "rxjs";
import {MsgResult} from "../../../models/msg-result";
import {API} from "../../../constants/constants";
import {Dictionary} from "./dictionary";
import {FormUtil} from "../../../util/form-util";

@Injectable()
export class DictionarysService {

  constructor(private requestUtil: RequestUtil,
              private formUtil: FormUtil,) {
  }

  /**
   *  无参查询初始化
   * @returns {Observable<MsgResult>|any}
   */
  treeDD(): Observable<MsgResult> {
    return this.requestUtil.get(API.treeDD);
  }

  /**
   *  添加字典表
   * @param dictionary
   * @returns {Observable<MsgResult>|any}
   */
  addDD(dic: Dictionary): Observable<MsgResult> {
    if (dic.pId == null) {
      dic.pId = '-1';
    }
    if (dic.describe == null) {
      dic.describe = '';
    }
    if (dic.value == null) {
      dic.value = '';
    }

    let params = 'id=' + dic.id + '&pId=' + dic.pId + '&display=' + dic.display + '&pName=' + dic.pName + '&code=' + dic.code + '&value=' + dic.value
      + '&describe=' + dic.describe + '&orderNum=' + dic.orderNum + '&isUse=' + dic.isUse;
    return this.requestUtil.post(API.addDD, params);
  }


  /**
   * 批量删除
   * @param ids
   * @returns {Observable<MsgResult>|any}
   */
  deletesDD(ids: Array<string>): Observable<MsgResult> {
    let params = "&ids=" + ids;
    return this.requestUtil.post(API.deletesDD, params);
  }

  /**
   * 根据id查询
   * @param id
   * @returns {Observable<MsgResult>|any}
   */
  getDDById(id): Observable<MsgResult> {
    return this.requestUtil.get(API.getDDById + "/" + id + "?format=json");
  }

  /**
   *  修改用户
   * @param dictionary
   * @returns {Observable<MsgResult>|any}
   */
  editDD(dic: Dictionary): Observable<MsgResult> {
    if (dic.pId == null) {
      dic.pId = '-1';
    }
    if (dic.describe == null) {
      dic.describe = '';
    }
    if (dic.value == null) {
      dic.value = '';
    }
    let params = this.formUtil.serialize(dic);
    return this.requestUtil.put(API.editDD + "/" + dic.id + "?format=json&" + params);

  }


}
